package com.gdth.bank.adapter;

import java.util.List;

import org.greenrobot.eventbus.EventBus;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.gdth.bank.R;
import com.gdth.bank.bean.CjkhItem;
import com.gdth.bank.event.CjkhjlListEvent;
import com.gdth.bank.utils.AppUtil;
import com.gdth.resource.Constant;
import com.gdth.utils.DateTimeUtil;
import com.gdth.utils.ImageUtil;

public class CjkhListViewAdapter extends BaseAdapter{
	protected Context context;  

 	protected List<CjkhItem> vList;
 	
 	protected int layout;
 	
 	public CjkhListViewAdapter(Context context, List<CjkhItem> vList) {  
        this.context = context;
        this.vList = vList;
       
        this.layout = R.layout.item_cjkh;
    }
 	
 	public CjkhListViewAdapter(Context context, List<CjkhItem> vList, int layout) {  
        this.context = context;
        this.vList = vList;
        
        this.layout = layout;
    }

    @Override 
    public int getCount() {  
        return vList.size();  
    }  

    @Override 
    public Object getItem(int position) {  
        return position;  
    }  

    @Override 
    public long getItemId(int position) {  
        return position;  
    }

	@Override 
    public View getView(int position, View convertView, ViewGroup parent) { 
		CjkhItem model = vList.get(position);
		ViewHolder holder = null;
		if (convertView == null){
			holder = new ViewHolder();
			
			convertView = LayoutInflater.from(context).inflate(layout, null);
			
			holder.local_tp = (ImageView) convertView.findViewById(R.id.cjkh_item_tp);
			holder.local_detail = (ImageView) convertView.findViewById(R.id.cjkh_item_detail);
		    holder.local_xm = (TextView) convertView.findViewById(R.id.cjkh_item_xm);
		    holder.local_sfz = (TextView) convertView.findViewById(R.id.cjkh_item_sfz);
		    holder.local_zt = (TextView) convertView.findViewById(R.id.cjkh_item_zt);
		    holder.local_cjsj = (TextView) convertView.findViewById(R.id.cjkh_item_cjsj);
		    holder.hand_upload = (ImageView)convertView.findViewById(R.id.hand_upload);
			holder.bflb_del = (TextView)convertView.findViewById(R.id.bflb_del);
			holder.bflb_change = (TextView)convertView.findViewById(R.id.bflb_change);
		    holder.line1 = convertView.findViewById(R.id.line1);
			
		    if (this.mOnCjjlDetailClickListener != null) {
		    	holder.local_detail.setOnClickListener(new OnClickListener(){

					@Override
					public void onClick(View v) {
						// TODO Auto-generated method stub
						int postition1 = Integer.valueOf(v.getTag().toString());
						mOnCjjlDetailClickListener.clickListener(postition1);
					}
		    		
		    	});
		    }
			convertView.setTag(holder);
        }
		
		else {
			holder = (ViewHolder) convertView.getTag();
		}
		
		if (holder != null){
			if (model.getImg() != null){
		    	ImageUtil.downloadAsyncTask(holder.local_tp, model.getImg());
		    }

		    holder.local_detail.setTag(position);
			holder.local_xm.setText(model.getValue());
			holder.local_sfz.setText(model.getSfz());
			holder.local_zt.setText(model.getTbzt() == AppUtil.sjzt_normal ? "已同步" : "未同步");
			holder.local_cjsj.setText(DateTimeUtil.getCurrentTime(model.getCjsj(), "yyyy-MM-dd"));
			if(Constant.isHandUpload && model.getTbzt() != AppUtil.sjzt_normal) {
		    	final String uuid = model.getKey();
		    	holder.line1.setVisibility(View.VISIBLE);
		    	holder.hand_upload.setVisibility(View.VISIBLE);
		    	holder.hand_upload.setOnClickListener(new OnClickListener() {
					
					@Override
					public void onClick(View v) {
						// TODO Auto-generated method stub
						EventBus.getDefault().post(new CjkhjlListEvent(CjkhjlListEvent.handUpload,uuid));
					}
				});
		    }else {
		    	holder.line1.setVisibility(View.GONE);
		    	holder.hand_upload.setVisibility(View.GONE);
		    }

			holder.bflb_del.setOnClickListener(new OnClickListener() {
				@Override
				public void onClick(View v) {
					EventBus.getDefault().post(new CjkhjlListEvent(CjkhjlListEvent.BFLB_DEL_CODE,model.getKey()));
				}
			});

			holder.bflb_change.setOnClickListener(new OnClickListener() {
				@Override
				public void onClick(View v) {
					EventBus.getDefault().post(new CjkhjlListEvent(CjkhjlListEvent.BFLB_CHANAGE_CODE,String.valueOf(position)));
				}
			});
		}
		
        return convertView;  
    }  

	public static class ViewHolder{
		public ImageView local_tp = null;
		public ImageView local_detail = null;
		public TextView local_xm = null;
		public TextView local_sfz = null;
		public TextView local_zt = null;
		public TextView local_cjsj = null;
		public ImageView hand_upload = null;
		public View line1 = null;
		public TextView bflb_del = null;
		private TextView bflb_change = null;
	}
	
	private OnCjjlDetailClickListener mOnCjjlDetailClickListener;
	
	public void setOnCjjlDetailClickListener(OnCjjlDetailClickListener mOnCjjlDetailClickListener) {
		this.mOnCjjlDetailClickListener = mOnCjjlDetailClickListener;
	}
	
	public interface OnCjjlDetailClickListener {
		void clickListener(int position);

	}
}
